from pattern.circuit import Control

class entry(Control):
    
    class TreeNode(object):
        def __init__(self, entry, event):
            self.entry = entry
            self.event = event
            
        def value(self):
            outlet = self.entry.acquireOutlet(self.event, "value")
            if outlet is None:
                return None
            else:
                return self.entry.getCaption(outlet)
                
        def childNodes(self):
            return [bridge.pull() for bridge in self.entry.acquireInterfaces(self.event, "child")]
            
        def __getattr__(self, nm):
            if hasattr(self.event.shape, nm):
                return getattr(self.event.shape, nm)
            else:
                bridge = self.entry.acquireInterface(self.event, nm)
                if bridge is not None:
                    return bridge.pull()
                else:
                    raise KeyError, "'%s' node has no attribute '%s'" % (self.value(), nm)

    def please(self, event):
        return self.TreeNode(self, event)
